(function(c) {
    var b = mobiscroll,
        a = b.$,
        d = {
            inputClass: '',
            values: 5,
            order: 'desc',
            style: 'icon',
            invalid: [],
            icon: {
                filled: 'star3',
                empty: 'star3'
            }
        };
    b.presetShort('rating');
    b.presets.scroller.rating = function(l) {
        var z = a.extend({}, l.settings),
            e = a.extend(l.settings, d, z),
            h = a(this),
            s = this.id + '_dummy',
            w = a('label[for="' + this.id + '"]').attr('for', s),
            A = e.label !== c ? e.label : w.length ? w.text() : h.attr('name'),
            p = e.defaultValue,
            v = [
                []
            ],
            x = {
                data: [],
                label: A,
                circular: false
            },
            n = {},
            g = [],
            i, r = false,
            f, k, o, t, u, y, j, q, B, m = e.style === 'grade' ? 'circle' : 'icon';
        if (h.is('select')) {
            e.values = {};
            a('option', h).each(function() {
                e.values[a(this).val()] = a(this).text();
            });
            a('#' + s).remove();
        }
        if (a.isArray(e.values)) {
            for (f = 0; f < e.values.length; f++) {
                j = +e.values[f];
                if (isNaN(j)) {
                    j = f + 1;
                    r = true;
                }
                g.push({
                    order: j,
                    key: e.values[f],
                    value: e.values[f]
                });
            }
        } else if (a.isPlainObject(e.values)) {
            f = 1;
            r = true;
            for (q in e.values) {
                j = +q;
                if (isNaN(j)) {
                    j = f;
                }
                g.push({
                    order: j,
                    key: q,
                    value: e.values[q]
                });
                f++;
            }
        } else {
            for (f = 1; f <= e.values; f++) {
                g.push({
                    order: f,
                    key: f,
                    value: f
                });
            }
        }
        if (e.showText === c && r) {
            e.showText = true;
        }
        if (e.icon.empty === c) {
            e.icon.empty = e.icon.filled;
        }
        g.sort(function(a, b) {
            return e.order == 'desc' ? b.order - a.order : a.order - b.order;
        });
        B = e.order == 'desc' ? g[0].order : g[g.length - 1].order;
        for (f = 0; f < g.length; f++) {
            y = g[f].order;
            t = g[f].key;
            u = g[f].value;
            o = '';
            for (k = 1; k < y + 1; k++) {
                o += '<span class="mbsc-rating-' + m + (m === 'circle' ? '' : ' mbsc-ic mbsc-ic-' + e.icon.filled) + ' ">' + (m == 'circle' ? k : ' ') + '</span>';
            }
            for (k = y + 1; k <= B; k++) {
                o += '<span class="mbsc-rating-' + m + (m === 'circle' ? ' mbsc-rating-circle-unf' : ' mbsc-ic mbsc-ic-' + (e.icon.empty ? e.icon.empty + ' mbsc-rating-icon-unf' : '') + (e.icon.empty === e.icon.filled ? ' mbsc-rating-icon-same' : '')) + '"></span>';
            }
            if (p === c) {
                p = t;
            }
            o += e.showText ? '<span class="mbsc-rating-txt">' + u + '</span>' : '';
            x.data.push({
                value: t,
                display: o,
                label: u
            });
            n[t] = u;
        }
        if (h.is('select')) {
            i = a('<input type="text" id="' + s + '" value="' + n[h.val()] + '" class="' + e.inputClass + '" placeholder="' + (e.placeholder || '') + '" readonly />').insertBefore(h);
        }
        v[0].push(x);
        if (i) {
            l.attachShow(i);
        }
        if (h.is('select')) {
            h.hide().closest('.ui-field-contain').trigger('create');
        }
        l.getVal = function(c) {
            var a = l._hasValue ? l[c ? '_tempWheelArray' : '_wheelArray'][0] : null;
            return b.util.isNumeric(a) ? +a : a;
        };
        return {
            anchor: i,
            wheels: v,
            headerText: false,
            compClass: 'mbsc-rating',
            setOnTap: true,
            formatValue: function(a) {
                return n[a[0]];
            },
            parseValue: function(b) {
                var a;
                for (a in n) {
                    if (i && a == b || !i && n[a] == b) {
                        return [a];
                    }
                }
                return [p];
            },
            validate: function() {
                return {
                    disabled: [e.invalid]
                };
            },
            onFill: function(a) {
                if (i) {
                    i.val(a.valueText);
                    h.val(l._tempWheelArray[0]);
                }
            },
            onDestroy: function() {
                if (i) {
                    i.remove();
                }
                h.show();
            }
        };
    };
}());